{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "90d5ba19",
   "metadata": {},
   "source": [
    "布尔值表示以下两个值之一：True 或 False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70b7b118",
   "metadata": {},
   "source": [
    "# 布尔值\n",
    "在编程中，您经常需要知道表达式是 True 还是 False。\n",
    "\n",
    "您可以在 Python 中计算任何表达式，并获得两个答案之一，True 或 False。\n",
    "\n",
    "当您比较两个值时，将计算表达式并且 Python 返回布尔值答案："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "61b0feca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print(10 > 9)\n",
    "print(10 == 9)\n",
    "print(10 < 9)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8e238b2",
   "metadata": {},
   "source": [
    "在 if 语句中运行条件时，Python 返回 True 或 False："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "211255ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b is not greater than a\n"
     ]
    }
   ],
   "source": [
    "a = 200\n",
    "b = 33\n",
    "\n",
    "if b > a:\n",
    "  print(\"b is greater than a\")\n",
    "else:\n",
    "  print(\"b is not greater than a\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6e4144c",
   "metadata": {},
   "source": [
    "# 评估值和变量\n",
    "bool() 函数允许您评估任何值，并返回 True 或 False，"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3e38fb9e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(bool(\"Hello\"))\n",
    "print(bool(15))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c28e52a0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "x = \"Hello\"\n",
    "y = 15\n",
    "\n",
    "print(bool(x))\n",
    "print(bool(y))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43799d76",
   "metadata": {},
   "source": [
    "# 大多数值是true\n",
    "如果它有某种内容，几乎任何值都会被评估为 True。\n",
    "\n",
    "任何字符串都是 True，除了空字符串。\n",
    "\n",
    "任何数字都是真，除了 0。\n",
    "\n",
    "任何列表、元组、集合和字典都是 True，除了空的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2a55e63d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool(\"abc\")\n",
    "bool(123)\n",
    "bool([\"apple\", \"cherry\", \"banana\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62e174d3",
   "metadata": {},
   "source": [
    "# 有些值是False\n",
    "实际上，除了空值（例如 ()、[]、{}、\"\"、数字 0 和值 None）之外，计算结果为 False 的值并不多。 当然，值 False 的计算结果为 False。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6ab15d77",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool(False)\n",
    "bool(None)\n",
    "bool(0)\n",
    "bool(\"\")\n",
    "bool(())\n",
    "bool([])\n",
    "bool({})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a91de292",
   "metadata": {},
   "source": [
    "在这种情况下，还有一个值或对象的计算结果为 False，也就是说，如果您有一个由具有 __len__ 函数且返回 0 或 False 的类构成的对象："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f57b6a6d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "class myclass():\n",
    "  def __len__(self):\n",
    "    return 0\n",
    "\n",
    "myobj = myclass()\n",
    "print(bool(myobj))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52ddcf15",
   "metadata": {},
   "source": [
    "# 函数可以返回布尔值\n",
    "您可以创建返回布尔值的函数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9de6c8a3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "def myFunction() :\n",
    "  return True\n",
    "\n",
    "print(myFunction())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5439bfd0",
   "metadata": {},
   "source": [
    "您可以根据函数的布尔值来执行代码："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c8baed0e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "YES!\n"
     ]
    }
   ],
   "source": [
    "def myFunction() :\n",
    "  return True\n",
    "\n",
    "if myFunction():\n",
    "  print(\"YES!\")\n",
    "else:\n",
    "  print(\"NO!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1918142b",
   "metadata": {},
   "source": [
    "Python 也有许多返回布尔值的内置函数，例如 isinstance() 函数，可用于确定对象是否属于某种数据类型："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "01c528d1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "x = 200\n",
    "print(isinstance(x, int))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
